package charles.testproject.xmlparser;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class DOMParserNamespaceFile {
	private static Logger logger = Logger.getLogger(DOMParser.class);
	public static void main(String[] args) {
		PropertyConfigurator.configure("log/log4jpropertiesfile/log4j.properties");
		System.out.println("***************************DOM PARSER***************************");
		long startTime = System.currentTimeMillis();
		try {
			logger.debug(" was called!");
			File f = new File("doc/Twitter&Calendar.xml");
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder  docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(f);
			NodeList tupleList = doc.getElementsByTagName("tuple");
			for(int i = 0; i < tupleList.getLength(); i++) {
				
				String idAttribute = null;
				Node temp = tupleList.item(i).getAttributes().getNamedItem("id");
				
				if(temp != null) {
					idAttribute = temp.getNodeValue();
				}
				//parser twitter's informations
				if(idAttribute.equals("twitter")){
					logger.debug("090827...parser twitter's informations");
					
					Node twitterTupleNode = tupleList.item(i);

					if (twitterTupleNode.getNodeType() != Node.ELEMENT_NODE) {
						logger.debug("locationTupleNode is not a node...");
						continue;
					}

					Element twitterElement = (Element) twitterTupleNode;
					// get <rpid:class> content
					NodeList rpidClassNotelist = twitterElement
							.getElementsByTagName("rpid:class");

					for (int j = 0; j < rpidClassNotelist.getLength(); j++) {
						String rpidClassValue = rpidClassNotelist.item(j)
								.getFirstChild().getNodeValue();
						System.out.println("rpidClassValue = " 
								+ rpidClassValue);
					}
					// get <contact> content
					NodeList twitterContactlist = twitterElement
							.getElementsByTagName("contact");

					for (int k = 0; k < twitterContactlist.getLength(); k++) {
						String twitterContactValue = twitterContactlist.item(k)
								.getFirstChild().getNodeValue();
						System.out.println("twitterContactValue = "
								+ twitterContactValue);
					}
					
				}
				//parser GoogleCalendar's informations
				else if(idAttribute.equals("GoogleCalendarTuple")) {
					logger.debug("090827...parser GoogleCalendar's informations");

					Node calendarTupleNode = tupleList.item(i);

					if (calendarTupleNode.getNodeType() != Node.ELEMENT_NODE) {
						logger.debug("locationTupleNode is not a node...");
						continue;
					}

					Element calendarElement = (Element) calendarTupleNode;
					// get <note> content
					NodeList calendarNotelist = calendarElement
							.getElementsByTagName("note");

					for (int m = 0; m < calendarNotelist.getLength(); m++) {
						String calendarNoteValue = calendarNotelist.item(m)
								.getFirstChild().getNodeValue();
						System.out.println("calendarNoteValue = " 
								+ calendarNoteValue);
					}
					// get <contact> content
					NodeList calendarContactlist = calendarElement
							.getElementsByTagName("contact");

					for (int n = 0; n < calendarContactlist.getLength(); n++) {
						String calendarContactValue = calendarContactlist.item(n)
								.getFirstChild().getNodeValue();
						System.out.println("calendarContactValue = "
								+ calendarContactValue);
					}
					
				}
				//parser Location's informations
				else if(idAttribute.equals("Location")) {
					logger.debug("parser Location's informations");
					
					Node locationTupleNode = tupleList.item(i);
					
					if(locationTupleNode.getNodeType() != Node.ELEMENT_NODE) {
						logger.debug("locationTupleNode is not a node...");
					}
					
					Element locationElement = (Element)locationTupleNode;
					
					NodeList locationlist = locationElement.getElementsByTagName("gml:coordinates");
					
				    for(int j = 0; j < locationlist.getLength(); j++){
				    	String locationInfo = locationlist.item(j).getFirstChild().getNodeValue();
				    	System.out.println("Location = " + locationInfo);
				    }
					
				}
				
			}
				
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("****DOMParser cost time = " + (endTime - startTime));
		System.out.println("***************************DOM PARSER***************************");
	}

}
